package com.hook3.pianke.activity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.view.ViewPager;
import android.support.v7.widget.Toolbar;

import android.view.View;
import android.widget.AdapterView;
import android.widget.FrameLayout;
import android.widget.GridView;

import android.widget.ScrollView;
import android.widget.TextView;


import com.google.gson.Gson;

import com.hook3.pianke.Adapter.ReadVGridViewAdapter;
import com.hook3.pianke.Adapter.ReadViewPagerAdapter;
import com.hook3.pianke.Net.Url;
import com.hook3.pianke.R;
import com.hook3.pianke.view.BaseReadDetailsActivity;

import org.xutils.common.Callback;
import org.xutils.http.RequestParams;
import org.xutils.x;


import java.io.Serializable;
import java.util.List;


/**
 * 阅读页面
 * Created by HOME on 2015/12/8.
 */
public class ReadingActivity {
    private Activity activity;
    private Toolbar toolbar;//标题
    private FrameLayout frameLayout;//实现View;
    private FrameLayout fl_read_image;

    public class JSON implements Serializable{
        public Data data ;
        public String result;
    }

    public class Data implements Serializable{
        public List<MainImageViewPager> carousel;
        public List<MainImageGridVIew> list;
    }

    public class MainImageViewPager implements Serializable {
        public String img;
        public String url;
    }

    public class MainImageGridVIew implements Serializable{
        public String coverimg;
        public String enname;
        public String name;
        public String type;
    }

    public ReadingActivity(Activity activity, Toolbar toolbar, FrameLayout frameLayout) {
        this.activity = activity;
        this.toolbar = toolbar;
        this.frameLayout = frameLayout;
        init();
    }

    public View view;

    // private ArrayList<ImageView> list = new ArrayList<>();
    private List<MainImageGridVIew> gridImageUrl;
    public TextView textView;
    public ScrollView sv_read_main;
    public List<MainImageViewPager> imageUrl;
    public JSON json;;
    private void init() {
        toolbar.setTitle("阅读");
        view = View.inflate(activity, R.layout.fl_read_main, null);
        fl_read_image = (FrameLayout) view.findViewById(R.id.fl_read_image);

        final ViewPager viewPager = (ViewPager) view.findViewById(R.id.vp_read_main);
        textView = (TextView) view.findViewById(R.id.tv_read_main);
        sv_read_main = (ScrollView) view.findViewById(R.id.sv_read_main);
        final GridView gridView = (GridView) view.findViewById(R.id.gv_read_main);


        RequestParams params = new RequestParams(Url.READ_MAIN);

        x.http().post(params, new Callback.CommonCallback<String>() {

            @Override
            public void onSuccess(String result) {

                textView.setVisibility(View.GONE);
                Gson gson = new Gson();
                final JSON json = gson.fromJson(result, JSON.class);

                imageUrl = json.data.carousel;
                gridImageUrl = json.data.list;
//                for(int i = 0;i < imageUrl.size();i ++) {
//                   ImageView imageView = new ImageView(activity);
//                    x.image().bind(imageView,imageUrl.get(i).img);
//                    list.add(imageView);
//                }
                gridView.setVerticalScrollBarEnabled(false);
                gridView.setAdapter(new ReadVGridViewAdapter(activity, gridImageUrl));
                viewPager.setAdapter(new ReadViewPagerAdapter(activity, imageUrl));


                gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                    @Override
                    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                        Intent intent = new Intent(activity, BaseReadDetailsActivity.class);
                        Bundle bundle = new Bundle();
                        bundle.putSerializable("json", json);
                        intent.putExtra("bjson", bundle);
                        intent.putExtra("position", position);
                        activity.startActivity(intent);


                    }
                });

                fl_read_image.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {

                    }
                });
                frameLayout.addView(sv_read_main);


            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                textView.setVisibility(View.VISIBLE);
            }

            @Override
            public void onCancelled(CancelledException cex) {

            }

            @Override
            public void onFinished() {

            }
        });


        //frameLayout.addView(View);
    }
}